package com.huizluo.module1;

import java.util.Scanner;
//根据输入打印当前日期是一年中的第几天
public class PrintDay {
    public static void main(String[] args) {
        System.out.println("请输入年月日,例如（2018-05-12）");
        Scanner scan = new Scanner(System.in);
        String date = scan.next();
        PrintDay p = new PrintDay();
        System.out.println("这天是一年中的第"+ p.NumDay(date) + "天");
    }

    private int NumDay(String date){
        boolean isLeapYear;
        int year,month,day,num = 0;
        String[] in = date.split("-");
        year = Integer.parseInt(in[0]);
        month = Integer.parseInt(in[1]);
        day = Integer.parseInt(in[2]);

        //判断是不是闰年
        isLeapYear = IsLeapYear(year);

        for (int i = 1;i < month ;i++){
            if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12){
                num = num + 31;
            }else if(i == 2){
                if (isLeapYear){
                    num = num + 29;
                }else{
                    num = num + 28;
                }
            }else{
                num = num + 30;
            }
        }

        return num + day;
    }
    //四年一润 百年不润 四百年在润
    private boolean IsLeapYear(int year){
        if ((year % 4) == 0){
            if (year % 400 == 0){
                return true;
            }else if (year % 100 == 0){
                return false;
            }else{
                return true;
            }
        }
        return false;
    }

}
